<?php
$reg = 100;
$pag = $_GET["p"];
$cri = $_GET["search"];

if(isset($_GET["dir"])) {
	$d = ($_GET["dir"]==0)? 1 : 0;	
} else {
	if(isset($_GET["d"])) { $d = $_GET["d"]; } else {	$d = 0;}
}   			   
if(isset($_GET["sort"])) {
   $sort = $_GET["sort"];
   $dir = ($d==0)? " ASC" : " DESC";
   $sort = substr($sort,0,1).$dir.substr($sort,1,strlen($sort));
} else {
   $sort = "1 ASC,2,3";
}
if (!$pag) {
	$ini = 0;
	$pag = 1;
} else {
	$ini = ($pag - 1) * $reg;
} 
ob_start();
require_once("db_conect.php");
/*
$qry = "select count(id) as total from libro where publicar=1";
$res = mysql_query($qry,$conexion);
$row = mysql_fetch_assoc($res);
$gen = $row["total"];

mysql_free_result($res);
*/
$qry  = "select libro.titulo, libro_comentario.por, libro.autor, libro_comentario.id  ";
$qry .= "from `libro` join `libro_comentario` on libro.id = libro_comentario.idlibro ";
/*
$res = mysql_query($qry,$conexion) or die(mysql_error());
$tot = mysql_num_rows($res);
$npa = ceil($tot / $reg);
mysql_free_result($res);
if ($tot < $reg) $reg = $tot;
*/
//$qry = "select titulo, autor, year, edicion,";
//$qry .= "isbn, paginas,id from libro ";
if (!$cri=="") 	$qry .= "where titulo like '%$cri%' or autor like '%$cri%'";
$qry .= "order by $sort";// LIMIT $ini , $reg";
//echo $qry;
$res = mysql_query($qry,$conexion) or die(mysql_error());

?>

<div id="bienvenida">
<!--div style="border:1px dotted #666; margin-top:10px; padding:4px; font-family:Verdana, Geneva, sans-serif; font-size:10px; margin-bottom:7px;">
<?php 
	//echo "(".$gen.") libros registrados. "; 
	//if ($tot!=$gen) echo $tot." Registros encontrados.";
	//mostras todos los registros (luego de una busqueda
	//if (!$cri=="") echo " <a href='index.php?id=2'>Ver Todo</a> ";
?>
</div-->
<table id="myBooks" width="100%">
<thead>
	<tr>
    	<th>#</th>
		<th><a href="index.php?id=3&sort=2,1,3&dir=<?php echo $d."&search=".$cri; ?>" class="header">T&iacute;tulo</a></th>
        <th><a href="index.php?id=3&sort=1,2,3&dir=<?php echo $d."&search=".$cri; ?>" class="header">Comentado por</a></th>
        <th><a href="index.php?id=3&sort=3,1,2&dir=<?php echo $d."&search=".$cri; ?>" class="header">Autor</a></th>
	</tr>
</thead>
<tbody>	
<?php
$s="";
while ($row2 = mysql_fetch_array($res, MYSQL_NUM)) {
	$n++;
	$ini++;
	echo "<tr class=\"$s\"><td>$ini</td>";
	$e = count($row2)-1;
	for($i=0; $i<$e; $i++)
	{
		if(is_numeric($row2[$i]))
		{
			$align = "align=\"right\"";
		} else {
			$align = "";
		}
		if ($i==0) {
			$sort = str_replace("ASC","",$sort);
			$sort = str_replace("DESC","",$sort);
			$valor = "<a id=\"$row2[6]\" href=\"show.coment.php?id=$row2[$e]&amp;sort=$sort&amp;dir=$d&search=$cri\">$row2[$i]</a>";
		} else {
			$valor = $row2[$i];
		}
		echo "<td $align >$valor</td>";
	}
	echo "</tr>";
	if($s==""){$s="odd";} else { $s="";}
	ob_flush();
}
?>
</tbody>
</table>
<div style="border:1px dotted #666; margin-top:10px; padding:4px; font-family:Verdana, Geneva, sans-serif; font-size:10px; text-transform:uppercase; text-align:center;">
<?php
if ($ini==0) {
	echo "<span class=\"noreg\"><br><br>A�n no se han agregado perlas literarias!!!<br><br><br></span>";
}
mysql_close($conexion);
?>
</div>
</div>